/**
 * 定义站点信息管理模块
 */
(function(window, angular, undefined){
	var Module = angular.module('water.apps.common.stationInfo.monitorStationManager', ['water.entity.monitorStation']);
	
	Module.controller('commonStationManagerController',function($rootScope,$scope,$monitorStationService){

		/**
		 * 编辑对话框对象
		 */
		$scope.editDialog;
		/**
		 * 详情对话框对象
		 */
		$scope.detailDialog;
		/**
		 * 新增对话框
		 */
		$scope.createDialog;
		/**
		 * 站点编辑对象，如果这里不先定义，界面会报错
		 */
		$scope.stationEdit = {};
		/**
		 * 站点新增对象
		 */
		$scope.stationCreate = {};
		/**
		 * 站点详情对象
		 */
		$scope.stationDetail = {};
		
		/**
		 * 监听站点列表点击事件
		 */
		$scope.dataPramChangedBroadCast = $scope.$on('dataPramChanged', function(event, data) {
			$scope._getStationsList(data.STATION_ID);
	    });
		
		/**
		 * 详情按钮点击事件
		 */
		$scope.onDetailClick = function(station){
			$monitorStationService.getModel(station.GUID).then(function(d){
				$scope.stationDetail = d;
				$scope.stationDetail.PROVINCE_CITE_AREA = $scope.stationDetail.PROVINCE_CODE_DESC+" "+$scope.stationDetail.CITY_CODE_DESC+" "+$scope.stationDetail.AREA_CODE_DESC;
				$scope.stationDetail.EVN_PHOTOS = _getImageList(station.EVN_PHOTO);
				$scope.detailDialog.open();
			});
		}
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(station){
			$monitorStationService.getModel(station.GUID).then(function(d){
				$scope.stationEdit = d;
			});
			$scope.editDialog.open();
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data){
			data.CODE = data.EXP_NO;
            console.log("onSubmitEditClick###",data);
			$monitorStationService.updateModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope._getStationsList();
				$scope.editDialog.close();
			});
		}
		/**
		 * 取消编辑按钮点击事件
		 */
		$scope.onCloseEditClick = function(){
			$scope.editDialog.close();
		}
		
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){
			$scope.stationCreate = {BC_COLLECTION_INFO_LIST:[{}],BUILD_STATUS:0};
			$scope.createDialog.open();
		}
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			//目前很多左侧的联动都还是通过code来标识站点，所以入库的时候必须负值code.
			data.CODE=data.EXP_NO;
			data.WWTP_ID=$scope.COMPANY_CODE;
			$monitorStationService.createModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
                $scope._getStationsList();
//				$scope.createDialog.close();
			});
		}
		/**
		 * 取消新建按钮点击事件
		 */
		$scope.onCloseCreateClick = function(){
			$scope.createDialog.close();
		}
		
		/**
		 * 获取站点列表
		 */
		$scope._getStationsList=function (stationId){
			$monitorStationService.getList({'STATION_ID':stationId}).then(function(d){
				$scope.stations = d;
			});
		}
		
		/**
		 * 处理环境图片
		 */
		function _getImageList(imgUrls){
			if(!imgUrls){
				return;
			}
			var imgs = imgUrls.split("|");
			for(var i=0;i<imgs.length;i++){
				imgs[i] = imgs[i].split(":")[0];
			}
			return imgs;
		}
		
	});
}(window, angular));	